//
//  StatusBarView.swift
//  知乎日报Demo
//
//  Created by Ann on 2017/10/19.
//  Copyright © 2017年 Ann. All rights reserved.
//

import UIKit


import UIKit
import SnapKit
protocol StatusBarActionDelegate:NSObjectProtocol {
    func popularityViewAnimate()
    func backListViewAction()
    func nextPageNewsAction()
}
//5.12详情页面ToolsBar
class StatusBarView: UIView {
    
    var extraInfo:NewsExtra?{
        didSet{
            commentInCount.text = String(extraInfo!.comments)
      }
    }
    var statusBarDelegate:StatusBarActionDelegate?
    
    var isPopularitySelected:Bool = false {
        didSet{
            support.isSelected = isPopularitySelected
            supporInCount.textColor = isPopularitySelected == false ? UIColor.gray: UIColor.init(red: 53/255, green: 188/255, blue: 245/255, alpha: 1)
        }
        
    }
    var popCount:Int?{
        didSet{
            supporInCount.text = String( popCount!)
        }
    }
    
    lazy var backListView:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named:"Comment_Icon_Back"), for: UIControlState.normal)
        btn.sizeToFit()
        
        return btn
        
    }()
    
    
    lazy var nextPageNews:UIButton = {
        let btn =   UIButton()
        btn.tintColor = UIColor.red
        btn.setImage(UIImage(named:"News_Navigation_Unnext"), for: UIControlState.normal)
        btn.sizeToFit()
        return btn
        
    }()
    
    lazy var support:UIButton = {
        let btn =   UIButton()
        
        btn.setImage(UIImage(named:"News_Navigation_Vote"), for: UIControlState.normal)
        
        btn.setImage(UIImage(named:"News_Navigation_Voted"), for: UIControlState.selected)
        
        btn.sizeToFit()
        return btn
    }()
    
   fileprivate lazy var supporInCount:UILabel = {
        let label = UILabel()
        label.text = ""
        label.font = UIFont.systemFont(ofSize: 8)
        label.layer.anchorPoint = CGPoint(x: 0, y: 1)
        label.textColor = UIColor.gray
        //label.textColor = UIColor.init(red: 53/255, green: 188/255, blue: 245/255, alpha: 1)
        label.sizeToFit()
        return label
        
    }()
    
    fileprivate lazy var shareIcon:UIButton = {
        let btn =   UIButton()
        btn.setImage(UIImage(named:"News_Navigation_Share"), for: UIControlState.normal)
        btn.sizeToFit()
        return btn
    }()
    
   fileprivate lazy var commentViewButton:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named:"News_Navigation_Comment"), for: UIControlState.normal)
        btn.sizeToFit()
        return btn
    }()
    
  fileprivate  lazy var commentInCount:UILabel = {
        let label = UILabel()
        label.text = "..."
        label.layer.anchorPoint = CGPoint(x: 0, y: 1)
        label.font = UIFont.systemFont(ofSize: 8)
        label.textColor = UIColor.white
        // label.backgroundColor = UIColor.red
        label.textAlignment = NSTextAlignment.center
        return label
        
    }()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: barHeight))
        
        setupUI()
        
        
    }
  
  fileprivate  func setupUI(){
        
        self.addSubview(backListView)
        addSubview(nextPageNews)
        addSubview(support)
        addSubview(supporInCount)
        addSubview(shareIcon)
        addSubview(commentViewButton)
        addSubview(commentInCount)
        let space = ScreenWidth/5
        
        backListView.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
        nextPageNews.frame = CGRect(x: space * 1, y: 0, width: 0, height: 0)
        support.frame = CGRect(x: space * 2, y: 0, width: 0, height: 0)
        shareIcon.frame = CGRect(x: space * 3, y: 0, width: 0, height: 0)
        commentViewButton.frame = CGRect(x: space * 4, y: 0, width: 0, height: 0)
        
        backListView.sizeToFit()
        nextPageNews.sizeToFit()
        support.sizeToFit()
        shareIcon.sizeToFit()
        commentViewButton.sizeToFit()
        
        setupSupportButton()
        setCommentLabel()
        commentInCount.sizeToFit()
        
        backListView.addTarget(self, action: #selector(self.backListViewAction), for: UIControlEvents.touchUpInside)
        nextPageNews.addTarget(self, action: #selector(self.nextPageNewsAction), for: UIControlEvents.touchUpInside)
        
        commentViewButton.addTarget(self, action: #selector(self.showCommnetViewAction), for: .touchUpInside)
        
    }
    
    
  fileprivate  func setupSupportButton(){
        //布局获取赞个数
        supporInCount.snp.makeConstraints { (make) in
            make.centerX.equalTo(support.snp.centerX).offset(10)
            make.centerY.equalTo(support.snp.centerY).offset(-5)
            
            
        }
        support.addTarget(self, action: #selector(self.acceptOrUnaccept), for: UIControlEvents.touchUpInside)
        
        // self.addSubview(acceptAnimateView)
    }
    
  @objc fileprivate  func acceptOrUnaccept(){
        self.statusBarDelegate?.popularityViewAnimate()
        
    }
    
   fileprivate func setCommentLabel(){
        
        commentInCount.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(commentViewButton.snp.centerX).offset(5)
            make.centerY.equalTo(commentViewButton.snp.centerY).offset(-3)
            
        }
    }
    
   @objc fileprivate func backListViewAction(){
        
        statusBarDelegate?.backListViewAction()
    }
    
   @objc fileprivate func nextPageNewsAction(){
        
        statusBarDelegate?.nextPageNewsAction()
    }
   @objc fileprivate func showCommnetViewAction(){
        NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "show_comment_view") , object: self)
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
